var validateExt = {
    initValidate: function(element,callback) {

        //初始化表单验证
        $(element).form({
            onSubmit:function(){
                var isValid = $(this).form('enableValidation').form('validate');
                if (!isValid) {          
                    return isValid;
                }
            },
            success:function(){
                if (callback!=undefined && callback!=null) {
                    callback();
                }
                return false;
            }
        });
    }
};

$.extend($.fn.validatebox.defaults.rules, {
    CHS: {
      validator: function (value, param) {
        return /^[\u0391-\uFFE5]+$/.test(value);
      },
      message: '请输入汉字'
    },
    english : {// 验证英语
          validator : function(value) {
              return /^[A-Za-z]+$/i.test(value);
          },
          message : '请输入英文'
      },
    ip : {// 验证IP地址
          validator : function(value) {
              return /\d+\.\d+\.\d+\.\d+/.test(value);
          },
          message : 'IP地址格式不正确'
      },
    ZIP: {
      validator: function (value, param) {
        return /^[0-9]\d{5}$/.test(value);
      },
      message: '邮政编码不存在'
    },
    QQ: {
      validator: function (value, param) {
        return /^[1-9]\d{4,10}$/.test(value);
      },
      message: 'QQ号码不正确'
    },
    mobile: {
      validator: function (value, param) {
        return /^(?:13\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/.test(value);
      },
      message: '手机号码不正确'
    },
    tel:{
      validator:function(value,param){
        return /^(\d{3}-|\d{4}-)?(\d{8}|\d{7})?(-\d{1,6})?$/.test(value);
      },
      message:'电话号码不正确'
    },
    mobileAndTel: {
      validator: function (value, param) {
        return /(^([0\+]\d{2,3})\d{3,4}\-\d{3,8}$)|(^([0\+]\d{2,3})\d{3,4}\d{3,8}$)|(^([0\+]\d{2,3}){0,1}13\d{9}$)|(^\d{3,4}\d{3,8}$)|(^\d{3,4}\-\d{3,8}$)/.test(value);
      },
      message: '请正确输入电话号码'
    },
    number: {
      validator: function (value, param) {
        return /^[0-9]+.?[0-9]*$/.test(value);
      },
      message: '请输入数字'
    },
    money:{
      validator: function (value, param) {
        return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
       },
       message:'请输入正确的金额'

    },
    mone:{
      validator: function (value, param) {
        return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
       },
       message:'请输入整数或小数'

    },
    integer:{
      validator:function(value,param){
        return /^[+]?[1-9]\d*$/.test(value);
      },
      message: '请输入最小为1的整数'
    },
    integ:{
      validator:function(value,param){
        return /^[+]?[0-9]\d*$/.test(value);
      },
      message: '请输入整数'
    },
    range:{
      validator:function(value,param){
        if(/^[1-9]\d*$/.test(value)){
          return value >= param[0] && value <= param[1]
        }else{
          return false;
        }
      },
      message:'输入的数字在{0}到{1}之间'
    },
    minLength:{
      validator:function(value,param){
        return value.length >=param[0]
      },
      message:'至少输入{0}个字'
    },
    maxLength:{
      validator:function(value,param){
        return value.length<=param[0]
      },
      message:'最多{0}个字'
    },
    selectValid:{
      validator:function(value,param){
            if (value == "" || value.indexOf('请选择') >= 0) 
            {   
                return false;  
            }else 
            {  
                return true;  
            } 
      },
      message:'请选择'
    },
    idCode:{
      validator:function(value,param){
        return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value);
      },
      message: '请输入正确的身份证号'
    },
    loginName: {
      validator: function (value, param) {
        return /^[\u0391-\uFFE5\w]+$/.test(value);
      },
      message: '登录名称只允许汉字、英文字母、数字及下划线。'
    },
    equalTo: {
      validator: function (value, param) {
        return value == $(param[0]).val();
      },
      message: '两次输入的字符不一至'
    },
    englishOrNum : {// 只能输入英文和数字
          validator : function(value) {
              return /^[a-zA-Z0-9_ ]{1,}$/.test(value);
          },
          message : '请输入英文、数字、下划线或者空格'
      },
     xiaoshu:{ 
        validator : function(value){ 
        return /^(([1-9]+)|([0-9]+\.[0-9]{1,2}))$/.test(value);
        }, 
        message : '最多保留两位小数！'    
        },
    ddPrice:{
        validator:function(value,param){
            if(/^[1-9]\d*$/.test(value)){
                return value >= param[0] && value <= param[1];
            }else{
                return false;
            }
        },
        message:'请输入1到100之间正整数'
      },
      jretailUpperLimit:{
        validator:function(value,param){
            if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
                return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
            }else{
                return false;
            }
        },
        message:'请输入0到100之间的最多俩位小数的数字'
      },
      rateCheck:{
        validator:function(value,param){
          if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
            return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
          }else{
            return false;
          }
        },
        message:'请输入0到1000之间的最多俩位小数的数字'
      }
});

// 当时间输入不合法时，禁用查询和导出按钮 luoxianglin 2016-06-23
(function ($) {
    if ($.fn != null && $.fn != undefined && $.fn.datebox != null && $.fn.datebox != undefined &&
    $.fn.datebox.defaults != null && $.fn.datebox.defaults != undefined) {
        $.extend($.fn.datebox.defaults.rules, {
            VerifyLegality: {
                validator: function (value, param) {
                    var bSuccess = true;
                    var DateRegExp = /^[1-2]\d{3}-(0{0,1}[1-9]|1[0-2]){1}-(0{0,1}[0-9]|[1-2][0-9]|3[0-1]){1,2}$/g;
                    if (!DateRegExp.test(value)) {
                        bSuccess = false;
                    }

                    if (value != undefined && value != "") {
                        var timeArr = value.split("-");
                        if (timeArr != null && timeArr.length == 3) {
                            for (var i = 0; i < timeArr.length; i++) {
                                if ((i == 0 && parseInt(timeArr[i]) > 9999) ||
                                (i == 1 && (parseInt(timeArr[i]) > 12 || parseInt(timeArr[i]) < 1)) ||
                                (i == 2 && (parseInt(timeArr[i]) > 31 || parseInt(timeArr[i]) < 1))) {
                                    bSuccess = false;
                                }
                            }
                        } else {
                            bSuccess = false;
                        }
                    }

                    var btnSearch = $('#btnSearch');
                    if (btnSearch != undefined && btnSearch.length > 0) {
                        if (bSuccess) {
                            btnSearch.linkbutton('enable');
                        } else {
                            btnSearch.linkbutton('disable');
                        }
                    }

                    var btnExport = $('#btnExport');
                    if (btnExport != undefined && btnExport.length > 0) {
                        if (bSuccess) {
                            btnExport.linkbutton('enable');
                        } else {
                            btnExport.linkbutton('disable');
                        }
                    }

                    var btnCheck = $('#btnCheck');
                    if (btnCheck != undefined && btnCheck.length > 0) {
                        if (bSuccess) {
                            btnCheck.linkbutton('enable');
                        } else {
                            btnCheck.linkbutton('disable');
                        }
                    }

                    var btnDataCmd = $('#btnDataCmd');
                    if (btnDataCmd != undefined && btnDataCmd.length > 0) {
                        if (bSuccess) {
                            btnDataCmd.linkbutton('enable');
                        } else {
                            btnDataCmd.linkbutton('disable');
                        }
                    }

                    return bSuccess;
                },
                message: "时间格式输入不正确"
            }
        });

        $.extend($.fn.datebox.defaults.rules, {
            VerifyLegalityEdit: {
                validator: function (value, param) {
                    var bSuccess = true;
                    var DateRegExp = /^[1-2]\d{3}-(0{0,1}[1-9]|1[0-2]){1}-(0{0,1}[0-9]|[1-2][0-9]|3[0-1]){1,2}$/g;
                    if (!DateRegExp.test(value)) {
                        bSuccess = false;
                    }

                    if (value != undefined && value != "") {
                        var timeArr = value.split("-");
                        if (timeArr != null && timeArr.length == 3) {
                            for (var i = 0; i < timeArr.length; i++) {
                                if ((i == 0 && parseInt(timeArr[i]) > 9999) ||
                                (i == 1 && (parseInt(timeArr[i]) > 12 || parseInt(timeArr[i]) < 1)) ||
                                (i == 2 && (parseInt(timeArr[i]) > 31 || parseInt(timeArr[i]) < 1))) {
                                    bSuccess = false;
                                }
                            }
                        } else {
                            bSuccess = false;
                        }
                    }

                    return bSuccess;
                },
                message: "时间格式输入不正确"
            }
        });
    }

    if ($.fn != null && $.fn != undefined && $.fn.validatebox != null && $.fn.validatebox != undefined &&
    $.fn.validatebox.defaults != null && $.fn.validatebox.defaults != undefined) {
        $.extend($.fn.validatebox.defaults.rules, {
            maxLength: {
                validator: function (value, param) {
                    var bSuccess = value.length <= param[0];

                    return bSuccess;
                },
                message: "最多输入{0}个字"
            }
        });
    }

    if ($.fn != null && $.fn != undefined && $.fn.linkbutton != null && $.fn.linkbutton != undefined &&
    $.fn.linkbutton.defaults != null && $.fn.linkbutton.defaults != undefined) {
        $.extend($.fn.linkbutton.methods, {
            /**
            * 激活选项（覆盖重写）
            * @param {Object} jq
            */
            enable: function (jq) {
                return jq.each(function () {
                    var state = $.data(this, 'linkbutton');
                    if ($(this).hasClass('l-btn-disabled')) {
                        var itemData = state._eventsStore;
                        //恢复超链接
                        if (itemData.href) {
                            $(this).attr("href", itemData.href);
                        }
                        //恢复点击事件
                        if (itemData.onclicks) {
                            for (var j = 0; j < itemData.onclicks.length; j++) {
                                $(this).bind('click', itemData.onclicks[j]);
                            }
                        }
                        //设置target为null，清空存储的事件处理程序
                        itemData.target = null;
                        itemData.onclicks = [];
                        $(this).removeClass('l-btn-disabled');
                    }
                });
            },
            /**
            * 禁用选项（覆盖重写）
            * @param {Object} jq
            */
            disable: function (jq) {
                return jq.each(function () {
                    var state = $.data(this, 'linkbutton');
                    if (!state._eventsStore)
                        state._eventsStore = {};
                    if (!$(this).hasClass('l-btn-disabled')) {
                        var eventsStore = {};
                        eventsStore.target = this;
                        eventsStore.onclicks = [];
                        //处理超链接
                        var strHref = $(this).attr("href");
                        if (strHref) {
                            eventsStore.href = strHref;
                            $(this).attr("href", "javascript:void(0)");
                        }
                        //处理直接耦合绑定到onclick属性上的事件
                        var onclickStr = $(this).attr("onclick");
                        if (onclickStr && onclickStr != "") {
                            eventsStore.onclicks[eventsStore.onclicks.length] = new Function(onclickStr);
                            $(this).attr("onclick", "");
                        }
                        //处理使用jquery绑定的事件
                        var eventDatas = $(this).data("events") || $._data(this, 'events');
                        if (eventDatas["click"]) {
                            var eventData = eventDatas["click"];
                            for (var i = 0; i < eventData.length; i++) {
                                if (eventData[i].namespace != "menu") {
                                    eventsStore.onclicks[eventsStore.onclicks.length] = eventData[i]["handler"];
                                    $(this).unbind('click', eventData[i]["handler"]);
                                    i--;
                                }
                            }
                        }
                        state._eventsStore = eventsStore;
                        $(this).addClass('l-btn-disabled');
                    }
                });
            }
        });
    }
})(jQuery);